package com.nbpt;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.nbpt.db.AccountDBHelp;

import javax.security.auth.login.LoginException;

public class SmartCity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener {

    private EditText zh;
    private EditText mm;
    private Button btnlogin;
    private CheckBox box;
    private SharedPreferences preferences;
    public  static String userid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_city);


        zh = findViewById(R.id.zhanghao);
        mm = findViewById(R.id.mima);
        btnlogin = findViewById(R.id.btn);
        btnlogin.setOnClickListener(this);

        box = findViewById(R.id.checkBox);
        box.setOnCheckedChangeListener(this);
        preferences = getPreferences(Context.MODE_PRIVATE);
        boolean flag = preferences.getBoolean("checkbox",false);
        String user=preferences.getString("username","");
        String pass=preferences.getString("password","");
        if (flag)
        {
            zh.setText(user);
            mm.setText(pass);
            box.setChecked(true);
        }

        String username="",password="";
        Intent intent=getIntent();
        if(intent !=null){
            username =intent.getStringExtra("username");
            password =intent.getStringExtra("password");
            if(username!=null&&password!=null)
            {
                zh.setText(username);
                mm.setText(password);
            }
        }

}


    public void zhuceclick(View view){
        Intent intent = new Intent(SmartCity.this,RegisterAcitvity.class);
        startActivityForResult(intent,0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==0)
        {
          String username =  data.getStringExtra("username");
          String password = data.getStringExtra("password");
          zh.setText(username);
          mm.setText(password);
        }
    }

    @Override
    public void onClick(View view) {
        String zhanghao = zh.getText().toString();
        String mima = mm.getText().toString();
        String tempuser ,temppassword;

        if(zhanghao.isEmpty()||mima.isEmpty())
        {
            Toast.makeText(SmartCity.this,"用户名和密码为空",Toast.LENGTH_LONG).show();
        }

//        else
//            {
//                if(ischeck)
//                {//通过SharedPreferences获取Editor对象
//                    SharedPreferences.Editor editor =preferences.edit();
//                    //把用户名、密码和开关添加到Editor中
//                    editor.putString("username","");
//                    editor.putString("password","");
//                    editor.putBoolean("checkbox",true);//进行保存
//                    editor.commit();
//                }
//                else
//                    {
//                        SharedPreferences.Editor editor =preferences.edit();//清空数据
//                        editor.clear();
//                    }
//            }

                    if ((!zhanghao.isEmpty())&&(!mima.isEmpty()))
                    {
                        AccountDBHelp helper = new AccountDBHelp(SmartCity.this,AccountDBHelp.VERSION_1);
                        Cursor cursor = helper.select(null,null);
                        cursor.moveToFirst();
                        boolean isRegister =false;                               //标记
                        for (int i =0; i<cursor.getCount();i++)
                        {
                            tempuser = cursor.getString(1);
                            temppassword =cursor.getString(2);
                            if(!tempuser.equals(zhanghao))
                            {
                                Toast.makeText(SmartCity.this, "账号未注册", Toast.LENGTH_LONG).show();
                                break;
                            }
                            else if(tempuser.equals(zhanghao)&&!temppassword.equals(mima))
                            {
                                isRegister = true;
                                Toast.makeText(SmartCity.this, "密码错误", Toast.LENGTH_LONG).show();
                                break;
                            }
                            if (tempuser.equals(zhanghao)&&temppassword.equals(mima))
                            {
                                Toast.makeText(SmartCity.this,"登陆成功",Toast.LENGTH_LONG).show();
                                userid=cursor.getString(0);
                                Intent intent = new Intent(SmartCity.this,denglu.class);
                                startActivity(intent);
                                xuni.xb=cursor.getString(5);//xb
                                xuni.phone=cursor.getString(3);//phone
                                xuni.username=cursor.getString(1);//username
                                SharedPreferences.Editor editor =preferences.edit();
                                //把用户名、密码和开关添加到Editor中
                                editor.putString("username",tempuser);
                                editor.putString("password",temppassword);
                                editor.commit();
                                break;
                            }
                            else
                            {
                                cursor.moveToNext();
                            }
                        }
//                        if (!isRegister)
//                        {
//                            Toast.makeText(SmartCity.this, "账号未注册", Toast.LENGTH_LONG).show();
//                        }
                    }
    }



    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        if (compoundButton.getId()== R.id.checkBox)
        {
            //保存b值
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("checkbox",b);
            editor.commit();
        }

    }
}
